//
//  EFGoodsCategoryViewController.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/28.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFGoodsCategoryViewController.h"
#import "EFGoodDetailViewItemGuessLike.h"
#import "iToast.h"

@interface EFGoodsCategoryViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(strong,nonatomic)UITableView* tableView;
@property(nonatomic, strong)NSMutableArray *dataArray;
@property(nonatomic, strong)UIImageView *topIV;

@property(nonatomic,strong)NSString* categoryId;

@end

@implementation EFGoodsCategoryViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (instancetype)init{
    self = [super init];
    if (self) {
        self.dataArray = [NSMutableArray array];
        self.title = @"商品分类";
    }
    return self;
}

-(instancetype)initWithCategoryId:(NSString *)categoryId{
    self = [self init];
    if(self){
        self.categoryId = categoryId;
    }
    return self;
}


-(void)initView{
    [self.view addSubview:self.topIV];
    [self.view addSubview:self.tableView];
}

-(void)initData{
    NSString* page = @"1";
    NSString* size = @"10";
    __weak typeof(self) weakSelf = self;
    [APP_CONTEXT.net getProductCategory:self.categoryId page:page size:size OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
          if(code == 0){
              NSString* topImg = [info objectForKey:@"categoryImg"];
              [weakSelf.topIV setImageWithURL:[NSURL URLWithString:topImg]]; 
              NSArray *array = [info objectForKey:@"productList"];
              for(int i =0 ; i < array.count; ++i){
                  EFGoodDetailViewItemGuessLike* guestItem = [[EFGoodDetailViewItemGuessLike alloc]init];
                  NSDictionary *leftDic = [array objectAtIndex:i];
                  EFSepecialItem* leftItem = [EFSepecialItem yy_modelWithDictionary:leftDic];
                  
                  int j = i+1;
                  if(j < array.count){
                      NSDictionary *rightDic = [array objectAtIndex:i];
                      EFSepecialItem* rightItem = [EFSepecialItem yy_modelWithDictionary:rightDic];
                      guestItem.leftItem = leftItem;
                      guestItem.rightItem = rightItem;
                      
                      [weakSelf.dataArray addObject:guestItem];
                      i= j;
                  }
              }
              [weakSelf.tableView reloadData];
          }else{
              [[iToast makeText:message]show];
          }
      }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem *item = self.dataArray[indexPath.row];
    
    return item.viewHeight;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem* item = self.dataArray[indexPath.row];
    NSString *identifier = NSStringFromClass(item.viewClass);
    ICTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil){
        cell = [[item.viewClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if ([item isKindOfClass:[ICTableViewItem class]]) {
        [cell setObject:item];
    }
    return cell;
}


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kStatusBarHeight - 164 )];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.top = self.topIV.bottom;
    }
    return _tableView;
}

-(UIImageView *)topIV{
    if(_topIV == NULL){
        _topIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 164)];
        _topIV.backgroundColor = RGBCOLOR(0xf3f3f3);
        _topIV.contentMode = UIViewContentModeScaleAspectFill;
        _topIV.clipsToBounds = YES;
    }
    return _topIV;
}



@end
